// pages/my/myIdentity /myIdentity .js
const app = getApp();
Page({

  /** 
   * 页面的初始数据
   */
  data: {
    name:"",
    identityNumber:'',
    firstImg:'',
    secondImg:'',
    isReal:true,
    identityPositive:null,
    identityOpposite:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    app.util.ajax({
      url:"api/User/authentication",
    },function(res){
      let data = res.data.data;
      if (data.state == '3') {
        _this.setData({
          name:data.true_name,
          identityNumber:data.card_number,
          firstImg:data.card_front_photo,
          identityPositive:data.card_front_photo,
          secondImg:data.card_back_photo,
          identityOpposite:data.card_back_photo,
          isReal:false
        })
      }else{
        wx.showToast({title:data.stateDes,icon:'none'})
      } 
      if(data.state == '1') {
        wx.redirectTo({url:'/pages/my/myIdentity/audit/audit'})
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  
  uploadImg:function(e){
    console.log(e,12312);
    let imgType = e.currentTarget.dataset.type,
        _this = this;
    wx.chooseImage({
      count:1,
      success:function(res){
        console.log(res,'图片选择');
        let filePath = res.tempFilePaths[0];
        wx.uploadFile({
          url: 'http://192.168.188.122:8080/api/User/uploadImg?token='+app.globalData.userInfo.token,
          filePath:filePath,
          name:'file',
          success:function(data){
            
            data = JSON.parse(data.data);
            console.log(data,'图片上传');
            if (data.code == 1) {
              let changeData;
              // changeData = imgType =='0' ? {firstImg:filePath} : {secondImg:filePath};
              if (imgType =='0') {
                changeData = {
                  firstImg:filePath,
                  identityPositive:data.data.imgUrl
                }
              }else{
                changeData = {
                  secondImg:filePath,
                  identityOpposite:data.data.imgUrl
                }
              }
              _this.setData(changeData);
            }else if (data.code == -1) {
              wx.setStorageSync('userInfo', '');
              wx.showModal({
                content:'登录超时，重新登录',
                success:function(data){
                  if (data.confirm) {
                    wx.switchTab({url:'/pages/my/my'});
                  }
                }
              })
            }else{
              wx.showToast({title:data.msg || '上传失败',icon:'none'});
            }
          }
        })
      }
    })
    // wx.uploadFile({
    //   url: '',
    //   filePath: '',
    //   name: '',
    // })
  },
  formSubmit:function(e){
    console.log(e,123213);
    let nameValue = e.detail.value.name,
        identityValue = e.detail.value.identity,
        identityReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
        _this = this;
    if (nameValue && identityValue) {
      if (identityReg.test(identityValue)) {
        app.util.ajax({
          url:'api/User/idCardConfirm',
          method:'POST',
          data:{
            card_number:identityValue,
            true_name:nameValue,
            card_front_photo:_this.data.identityPositive,
            card_back_photo:_this.data.identityOpposite
          }
        },function(res){
          console.log(res,'上传结果')
        })
      }else{
        wx.showToast({
          title:'身份证格式错误',
          icon:'none'
        })
      }
    }else{
      wx.showToast({
        title:'请正确输入',
        icon:'none'
      })
    } 
  }
})